<template>
  <div class="search-history">
    <van-cell title="历史记录">
      <!-- 默认插槽 插入到单元格最右边 -->
      <div v-if="isDeleteShow">
        <!-- 
          prop 数据如果是引用类型（数组、对象）可以这样修改：
          user.name = 'jack'、arr.push(123)、arr.splice(0,1) 等
          prop 数据 不能重新赋值：xxx = xxx
          要想让 prop 数据 = 新的数据：让父组件自己修改
         -->
        <span @click="$emit('update-histories', [])">全部删除</span>&nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true"></van-icon>
    </van-cell>

    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="$emit('search', item)"
    >
      <van-icon
        name="close"
        v-show="isDeleteShow"
        @click.stop="onDelete(index)"
      />
      <!-- click.stop 阻止事件冒泡 -->
    </van-cell>
  </div>
</template>

<script>
// import { setItem } from "@/utils/storage";

export default {
  name: "searchHistory",
  props: {
    searchHistories: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isDeleteShow: false,
    };
  },
  computed: {},
  methods: {
    onDelete(index) {
      this.searchHistories.splice(index, 1);
      // 无论是否登录都把数据持久化到本地
      // 只有删除所有历史记录的接口 接口有问题 操作本地
      // setItem("search-histories", this.searchHistories);
      return;
    },
  },
};
</script>
<style lang='less' scoped>
</style>